package com.tnovoselec.android.travelassistant.dto;

import java.security.GeneralSecurityException;
import java.text.DecimalFormat;

import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.nth.android.Util;
import com.tnovoselec.android.lbs.location.DistanceCalculator;

@DatabaseTable(tableName = "bankomati")
public class BankomatDto implements IGeoPoint{

    public static final String DECIMAL_FORMAT_KM = "##,##0.## km";
    public static final String DECIMAL_FORMAT_M = "##,##0 m";

    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";

    @DatabaseField(id = true, canBeNull = false, dataType = DataType.STRING)
    private String poi_id = null;

    @DatabaseField(canBeNull = false, dataType = DataType.STRING)
    String grad;

    @DatabaseField(canBeNull = false, dataType = DataType.STRING)
    String adresa;

    String description;

    @DatabaseField(canBeNull = false, dataType = DataType.STRING)
    String bank_id;

    @DatabaseField(canBeNull = false, dataType = DataType.FLOAT)
    float longitude;

    @DatabaseField(canBeNull = false, dataType = DataType.FLOAT)
    float latitude;

    public String getPoi_id() {
	return poi_id;
    }

    public void setPoi_id(String poi_id) {
	this.poi_id = poi_id;
    }

    public String getGrad() {
	return grad;
    }

    public void setGrad(String grad) {
	this.grad = grad;
    }

    public String getAdresa() {
	return adresa;
    }

    public void setAdresa(String adresa) {
	this.adresa = adresa;
    }

    public String getDescription() {
	return description;
    }

    public void setDescription(String description) {
	this.description = description;
    }

    public String getBank_id() {
	return bank_id;
    }

    public void setBank_id(String bank_id) {
	this.bank_id = bank_id;
    }

    public float getLongitude() {
	return longitude;
    }

    public void setLongitude(float longitude) {
	this.longitude = longitude;
    }

    public float getLatitude() {
	return latitude;
    }

    public void setLatitude(float latitude) {
	this.latitude = latitude;
    }

    public static String generateID(String type, int i) throws GeneralSecurityException {
	return Util.md5hex(type + i);
    }

    public static String getCreateQuery() {
	return "CREATE TABLE IF NOT EXISTS `bankomati` (" + "`poi_id` INTEGER NOT NULL  PRIMARY KEY," + "`longitude` REAL NOT NULL," + "`latitude` REAL NOT NULL," + "`bank_id` TEXT NOT NULL,"
		+ "`grad` TEXT NOT NULL," + "`adresa` text NOT NULL) ;";
    }

    private double distance = -1;
    private String distanceText = null;

    public double getDistance(GeoPoint myLocation) {
	calculateDistance(myLocation);
	return distance;
    }

    public GeoPoint getGeoPoint() {

	return new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
    }

    public String getDistanceText() {
	return distanceText;
    }

    public String getDistanceText(GeoPoint myLocation) {
	calculateDistance(myLocation);
	return distanceText;
    }

    private void calculateDistance(GeoPoint myLocation) {
	if ((myLocation == null) || (distance != -1)) {
	    return;
	}

	distance = DistanceCalculator.distance(myLocation, getGeoPoint());
	if (distance >= 1000.0) {
	    distanceText = (new DecimalFormat(DECIMAL_FORMAT_KM)).format(distance / 1000.0);
	} else {
	    distanceText = (new DecimalFormat(DECIMAL_FORMAT_M)).format(distance);
	}
    }

    @Override
    public boolean equals(Object o) {
	if (this.poi_id.equals(((BankomatDto) o).getPoi_id())) {
	    Log.i("Equals:", "dto found!");
	    return true;

	}

	return false;
    }

    @Override
    public int compareTo(IGeoPoint another) {
	
	return getBank_id().compareTo(another.getBank_id());
    }
}
